package com.ruoyi.business.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.business.domain.BusDevicePressureItem;
import com.ruoyi.business.mapper.BusDevicePressureMapper;
import com.ruoyi.business.domain.BusDevicePressure;
import com.ruoyi.business.service.IBusDevicePressureService;
import javax.annotation.Resource;

/**
 * 调压设施台账Service业务层处理
 * 
 * @author wf
 * @date 2024-07-17
 */
@Service
public class BusDevicePressureServiceImpl implements IBusDevicePressureService 
{

    @Resource
    private BusDevicePressureMapper busDevicePressureMapper;

    /**
     * 查询调压设施台账
     * 
     * @param id 调压设施台账主键
     * @return 调压设施台账
     */
    @Override
    public BusDevicePressure selectBusDevicePressureById(Long id)
    {
//        查询有问题  子表的数据也要查找
        return busDevicePressureMapper.selectBusDevicePressureById(id);
    }

    /**
     * 查询调压设施台账列表
     * 
     * @param busDevicePressure 调压设施台账
     * @return 调压设施台账
     */
    @Override
    public List<BusDevicePressure> selectBusDevicePressureList(BusDevicePressure busDevicePressure)
    {
//        空属性  ，后面查询赋值
//        System.out.println("=============================" +  busDevicePressure.toString());

        return busDevicePressureMapper.selectBusDevicePressureList(busDevicePressure);
    }

    /**
     * 新增调压设施台账
     * 
     * @param busDevicePressure 调压设施台账
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusDevicePressure(BusDevicePressure busDevicePressure)
    {
        System.out.println("==============================================" + busDevicePressure.toString());
        busDevicePressure.setCreateUser(SecurityUtils.getUserId());
        busDevicePressure.setCreateBy(SecurityUtils.getUsername());
        busDevicePressure.setCreateTime(DateUtils.getNowDate());
        busDevicePressure.setUpdateUser(SecurityUtils.getUserId());
        busDevicePressure.setUpdateBy(SecurityUtils.getUsername());
        busDevicePressure.setUpdateTime(DateUtils.getNowDate());

        int rows = busDevicePressureMapper.insertBusDevicePressure(busDevicePressure);

        insertBusDevicePressureItem(busDevicePressure);
//      获取 对象的小区id
        Long neighborhoodId = busDevicePressure.getNeighborhoodId();
        if (StringUtils.isNotNull(neighborhoodId)) {

//            busDevicePressureMapper.updateNeighborhoodId(neighborhoodId);
        }

        return rows;
    }

    /**
     * 修改调压设施台账
     * 
     * @param busDevicePressure 调压设施台账
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBusDevicePressure(BusDevicePressure busDevicePressure)
    {

        busDevicePressure.setUpdateUser(SecurityUtils.getUserId());
        busDevicePressure.setUpdateBy(SecurityUtils.getUsername());
        busDevicePressure.setUpdateTime(DateUtils.getNowDate());
//
//        删除相关id的所有数据
        busDevicePressureMapper.deleteBusDevicePressureItemById(busDevicePressure.getId());
//        主子表同时插入数据  先插入主表，并获取主表的id 在插入子表，插入子表的时候，需要把主表的id传过去
//      报错原因： 子表插入时带着主键，插入失败
        insertBusDevicePressureItem(busDevicePressure);
//        修改主表
        return busDevicePressureMapper.updateBusDevicePressure(busDevicePressure);
    }

    /**
     * 批量删除调压设施台账
     * 
     * @param ids 需要删除的调压设施台账主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusDevicePressureByIds(Long[] ids)
    {
        busDevicePressureMapper.deleteBusDevicePressureItemByIds(ids);
        return busDevicePressureMapper.deleteBusDevicePressureByIds(ids);
    }

    /**
     * 删除调压设施台账信息
     * 
     * @param id 调压设施台账主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusDevicePressureById(Long id)
    {
        busDevicePressureMapper.deleteBusDevicePressureItemById(id);
        return busDevicePressureMapper.deleteBusDevicePressureById(id);
    }

    /**
     * 功能描述：获取调压设施总数量
     *
     * @return {@link Integer }
     * @author ling
     * @date 2024/08/22 15:28
     */
    @Override
    public Integer getCount() {
        return busDevicePressureMapper.getCount();
    }

    @Override
    public Integer getStationCount() {
        return busDevicePressureMapper.getStationCount();
    }

    @Override
    public Integer getCabinetCount() {
        return busDevicePressureMapper.getCabinetCount();
    }

    @Override
    public Integer getBoxCount() {
        return busDevicePressureMapper.getBoxCount();
    }

    @Override
    public Integer getGeotoneCount() {
        return busDevicePressureMapper.getGeotoneCount();
    }

    @Override
    public Long getCountByNeighborhoodId(Long neighborhoodId) {
        return busDevicePressureMapper.getCountByNeighborhoodId(neighborhoodId);
    }

    @Override
    public List<Long> getCountByNeighborhoodIds(Long[] neighborhoodId) {
        return busDevicePressureMapper.getCountByNeighborhoodIds(neighborhoodId);
    }

    /**
     * 新增调压设施工艺信息信息
     * 
     * @param busDevicePressure 调压设施台账对象
     */
    public void insertBusDevicePressureItem(BusDevicePressure busDevicePressure)
    {
        List<BusDevicePressureItem> busDevicePressureItemList = busDevicePressure.getBusDevicePressureItemList();
        Long id = busDevicePressure.getId();
        if (StringUtils.isNotNull(busDevicePressureItemList))
        {
            List<BusDevicePressureItem> list = new ArrayList<BusDevicePressureItem>();
            for (BusDevicePressureItem busDevicePressureItem : busDevicePressureItemList)
            {
                busDevicePressureItem.setDeivceId(id);
                list.add(busDevicePressureItem);
            }
            if (list.size() > 0)
            {
                busDevicePressureMapper.batchBusDevicePressureItem(list);
            }
        }
    }

//    public void updateBUsDeviceCount(Long neighborId){
//        //        查询小区的楼栋数
//        if (StringUtils.isNotNull(neighborId)){
//            Long count = busDevicePressureMapper.getCountByNeighborId(neighborId);
//            BusNeighborhoodLedger busNeighborhoodLedger = new BusNeighborhoodLedger(neighborId, count);
////        更新小区楼栋数
//            busNeighborhoodLedgerService.updateBusNeighborhoodLedger(busNeighborhoodLedger);
//        }
//    }
}
